<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card v-loading="loading">
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <div class="role-wrap">
              <el-row style="margin-bottom:10px">
                <el-button type="primary" size="small" @click="showDialog">
                  <i class="el-icon-plus" />
                  新增角色</el-button>
              </el-row>
              <el-table
                :border="true"
                :data="Tabledata"
                style="width: 100%"
              >
                <el-table-column type="index" label="序号" width="120" />
                <el-table-column prop="name" label="角色名" width="120" />
                <el-table-column prop="description" label="描述" />
                <el-table-column label="操作" width="240">
                  <template slot-scope="scope">
                    <el-button size="small" type="success">分配权限</el-button>
                    <el-button size="small" type="primary" @click="editRole(scope.row)">编辑</el-button>
                    <el-button size="small" type="danger" @click="delRole(scope.row.id)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-tab-pane>
          <!-- 公司信息 -->
          <el-tab-pane label="公司信息" name="second">
            <el-row type="flex" justify="center">
              <el-form ref="form" :model="form" label-width="80px" style="width:50%">
                <el-form-item label="企业名称">
                  <el-input v-model="form.name" disabled />
                </el-form-item>
                <el-form-item label="企业地址">
                  <el-input v-model="form.companyAddress" disabled />
                </el-form-item>
                <el-form-item label="企业电话">
                  <el-input v-model="form.companyPhone" disabled />
                </el-form-item>
                <el-form-item label="企业邮箱">
                  <el-input v-model="form.mailbox" disabled />
                </el-form-item>
                <el-form-item label="企业备注">
                  <el-input v-model="form.remarks" disabled />
                </el-form-item>
              </el-form>
            </el-row>
          </el-tab-pane>
        </el-tabs>
        <el-row type="flex" justify="center">
          <el-pagination
            layout="prev, pager, next"
            :total="total"
            :current-page="page"
            :page-size="pagesize"
            @current-change="pageChange"
          />
        </el-row>
      </el-card>
      <el-dialog
        :title="Roleform.id?'编辑角色':'新增角色'"
        :visible.sync="dialogVisible"
        width="width"
        @close="close"
      >
        <div>
          <el-form ref="addform" :rules="rules" :model="Roleform" label-width="80px">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="Roleform.name" placeholder="" />
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
              <el-input v-model="Roleform.description" placeholder="" />
            </el-form-item>
          </el-form>
        </div>
        <div slot="footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getConpany, getAllRole, addAllRole, delAllRole, getAllRoleById, updateAllRole } from '@/api/setting'
export default {
  name: 'Setting',
  data() {
    return {
      dialogVisible: false,
      loading: false,
      activeName: 'first',
      form: {},
      Tabledata: [],
      page: 1,
      pagesize: 10,
      total: null,
      Roleform: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, trigger: 'blur', message: '角色名称必填' }],
        description: [{ required: true, trigger: 'blur', message: '角色描述必填' }]
      }
    }
  },
  created() {
    this.getConpany()
    this.getAllRole()
  },
  methods: {
    async getConpany() {
      this.form = await getConpany(this.$store.getters.userInfo.companyId)
    },
    async getAllRole() {
      this.loading = true
      const { rows, total } = await getAllRole({
        page: this.page,
        pagesize: this.pagesize
      })
      this.Tabledata = rows
      this.total = total
      this.loading = false
    },
    pageChange(page) {
      this.page = page
      this.getAllRole()
    },
    showDialog() {
      this.dialogVisible = true
    },
    close() {
      this.$refs.addform.resetFields()
      this.Roleform = {
        name: '',
        description: ''
      }
      this.dialogVisible = false
    },
    async submit() {
      try {
        await this.$refs.addform.validate()
        if (this.Roleform.id) {
          await updateAllRole(this.Roleform)
        } else {
          await addAllRole(this.Roleform)
        }
        this.getAllRole()
        this.$message('操作成功')
        this.close()
      } catch (err) {
        console.log(err)
      }
    },
    async delRole(id) {
      console.log(id)
      try {
        await this.$confirm('确定删除嘛')
        await delAllRole(id)
        await this.getAllRole()
        this.$message.success('删除成功')
      } catch (err) {
        console.log(err)
      }
    },
    async editRole(data) {
      this.Roleform = await getAllRoleById(data.id)
      this.dialogVisible = true
    }

  }

}
</script>

<style>
.role-wrap{
  padding: 50px 30px;
}
</style>
